Function.RegisterNamespace("System.IO.DirectoryStrategy.SpiderMonkey");

System.IO.DirectoryStrategy.SpiderMonkey.Posix=function(){
    // IDirectoryStrategy Members
    this.Exists=function(path){
        return !!path&&System.Environment.Execute("sh",["-c",String.Format("\"test -d {0} && echo 1 || echo 0\"",normalizePath(path))])==1;
    };

    this.GetFiles=function(path){
        return String.Trim(System.Environment.Execute("find", [normalizePath(path),"-maxdepth","1","-type","f","-print"])).split("\n");
    };
    
    this.GetDirectories=function(path){
        var directories=String.Trim(System.Environment.Execute("find", [normalizePath(path),"-maxdepth","1","-type","d","-print"])).split("\n");
        for(var i=0;i<directories.length;i++){
            if(directories[i]==path)directories.splice(i--,1);
        }
        return directories;
    };

    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        if(typeof(system)!='undefined' && typeof(version)!='undefined' && typeof(snarf)!="undefined"){
            try{
                var exitCode=system("uname > NUL 2>&1");
                if(exitCode==0){
                    system("rm -f NUL");
                    return true;
                }
            }catch(e){}
        }
        return false;
    };

    // Private Methods
    function normalizePath(path){
        return String.Format("'{0}'",path);
    }
};

System.IO.DirectoryStrategy.SpiderMonkey.Posix.Implement(System.IO.DirectoryStrategy.IDirectoryStrategy,'System.IO.DirectoryStrategy.SpiderMonkey.Posix');
System.IO.DirectoryStrategy.SpiderMonkey.Posix.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.DirectoryStrategy.SpiderMonkey.Posix');

System.IO.Directory.Strategies.Add(System.IO.DirectoryStrategy.SpiderMonkey.Posix);